using System;
using System.Threading.Tasks;
using Gj.Galaxy.Logic;
using SimpleJSON;
using UnityEngine;

namespace Gj.DarkLoong
{
    public class Auth
    {
        public static string SessionToken = "";
        public static string Id = "";
        public static string UserId = "";

        public static bool IsLogin()
        {
            return !string.IsNullOrEmpty(SessionToken);
        }

        public static void Logout()
        {
            SessionToken = "";
            Id = "";
            UserId = "";
        }

        public static async Task<bool> Country(string country)
        {
            var header = Api.CookieHeader();
            header["token"] = Auth.SessionToken;
            var data = new JSONObject();
            data["country"] = country;
            var response = await Api.Post("/auth/country", data, header);
            return response.AsObject;
        }

        public static async Task<JSONObject> Login(string username, string password)
        {
            var header = Api.BaseHeader();
            var data = new JSONObject();
            data["username"] = username;
            data["password"] = password;
            var response = await Api.Post("/auth/login", data, header);
            return response.AsObject;
        }
        
        public static async Task<JSONObject> Token(string token)
        {
            var header = Api.BaseHeader();
            var data = new JSONObject();
            data["token"] = token;
            var response = await Api.Post("/auth/auth", data, header);
            return response.AsObject;
        }
        
        public static async Task<JSONObject> Guest()
        {
            var header = Api.BaseHeader();
            var data = new JSONObject();
            var response = await Api.Post("/auth/guest", data, header);
            return response.AsObject;
        }
        public static async Task<JSONObject> AppOfflineOauth(string channel, string content)
        {
            var header = Api.BaseHeader();
            var data = new JSONObject();
            data["channel"] = channel;
            data["content"] = content;
            var response = await Api.Post("/auth/appOfflineOauth", data, header);
            return response.AsObject;
        }

        public static async Task<JSONObject> Oauth(string channel, string code, string state)
        {
            var header = Api.BaseHeader();
            var data = new JSONObject();
            data["channel"] = channel;
            data["code"] = code;
            data["state"] = state;
            var response = await Api.Post("/auth/oauth", data, header);
            return response.AsObject;
        }

        public static async Task<JSONObject> AppOauth(string channel, string openId, string accessToken)
        {
            var header = Api.BaseHeader();
            var data = new JSONObject();
            data["channel"] = channel;
            data["openId"] = openId;
            data["accessToken"] = accessToken;
            var response = await Api.Post("/auth/appOauth", data, header);
            return response.AsObject;
        }

        public static async Task<JSONObject> Online()
        {
            var header = Api.CookieHeader();
            header["token"] = Auth.SessionToken;
            var data = new JSONObject();
            var response = await Api.Post($"/auth/online", data, header);
            return response.AsObject;
        }
        public static async Task<JSONObject> GetLoginCode()
        {
            var header = Api.CookieHeader();
            header["token"] = Auth.SessionToken;
            var data = new JSONObject();
            var response = await Api.Get($"/auth/loginCode", data, header);
            return response.AsObject;
        }
        
        public static void GetLoginCode(Action<JSONObject, string> cb)
        {
            Project.Api(GetLoginCode(), Api.AutoResult, cb);
        }
        
        public static async Task<JSONObject> LoginCode(string code)
        {
            var header = Api.BaseHeader();
            var data = new JSONObject();
            data["code"] = code;
            var response = await Api.Post($"/auth/loginCode", data, header);
            return response.AsObject;
        }
        
        public static async Task<JSONObject> ChangeInfo(string name, string avatar)
        {
            var header = Api.CookieHeader();
            header["token"] = Auth.SessionToken;
            var data = new JSONObject();
            data["nickName"] = name;
            data["avatar"] = avatar;
            var response = await Api.Post($"/auth/changeNickName", data, header);
            return response.AsObject;
        }

        public static async Task<JSONObject> accessToken(string Namespace, string mode)
        {
            var header = Api.BaseHeader();
            header["token"] = Auth.SessionToken;
            var data = new JSONObject();
            data["namespace"] = Namespace;
            data["mode"] = mode;
            var response = await Api.Post($"/auth/accessToken", data, header);
            var err = response["status"].AsInt != 200 ? response["message"].Value : "";

            return response.AsObject;
        }
    }
}